#include "init_show.h"

init_Show::init_Show(QWidget *parent) : QMainWindow(parent)
{
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
    this->setFixedSize(640,430);
    //定时器
    timer =new QTimer(this);
    timer->start(1000);
    for(int i=0;i<14;i++)//创建主屏幕显示的按键
    {
         theLabel[i] = new QLabel(this);
         theLabel[i]->setStyleSheet("QLabel{\
                                outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                                border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                                border-style: outset;\
                                /* 按钮文本设置 */\
                                qproperty-alignment: 'AlignCenter| AlignCenter';			             /* 文本的位置 */\
                                font-size: 50px;\
                                color: #000000;			                 /* 文本颜色 */\
                           }"
          );
    }

theLabel[12]->setStyleSheet("QLabel{ image: url(:/pic/logo.png); background-color: rgb(20, 170, 225);}"    );
theLabel[13]->setStyleSheet("QLabel{"
                      + the_StyleSheet[20]+ " font-size: 25px;"+ "background-color: rgb(20, 70, 255);	    /* 背景颜色 */"+  "}"    );
theLabel[12]->setGeometry(0,0,150,60);//设定每隔放置一个按键
theLabel[13]->setGeometry(150,0,490,60);//设定每隔放置一个按键
theLabel[13]->setText("数据           ");


//实时显示气体浓度
show_Data();

    QGridLayout *mainLayout = new QGridLayout();
    for(int i=0;i<3;i++)
    {
        mainLayout->addWidget(theLabel[i],1, i);
        mainLayout->addWidget(theLabel[3+i], 2, i);
        mainLayout->addWidget(theLabel[3*2+i], 3, i);
        mainLayout->addWidget(theLabel[3*3+i], 4, i);
    }
    mainLayout->setContentsMargins(50,110,50,0);//(int left, int top, int right, int bottom)
    mainLayout->setSizeConstraint(QLayout::SetMinimumSize);//布局显示效果
    QWidget* widget = new QWidget(this);
    widget->setGeometry(0,0,640,380);
    widget->setLayout(mainLayout);

    //时间实时更新
    connect(timer,&QTimer::timeout,[=](){
show_Data();
    });
}

    void init_Show::show_Data()
    {
        theLabel[0]->setText("总流量");
        theLabel[1]->setText(data_totalflow);
        theLabel[2]->setText("SCCM");
        theLabel[3]->setText(gas_RN[0]);
        theLabel[4]->setText(gas_RN[1]);
        theLabel[5]->setText(gas_RN[2]);
        theLabel[6]->setText(O3_RN[0]);
        theLabel[7]->setText(O3_RN[1]);
        theLabel[8]->setText(O3_RN[2]);
        theLabel[9]->setText(Photometer_RN[0]);
        theLabel[10]->setText(Photometer_RN[1]);
        theLabel[11]->setText(Photometer_RN[2]);
    }


    bool init_Show::eventFilter(QObject *watched, QEvent *event)
    {
//        if(watched == ui->pushButton)
        {
            if(event->type() == QEvent::KeyPress)
            {
               QKeyEvent *ev = (QKeyEvent *)event;
               //QPushButton *button = (QPushButton *)watched;
               switch (ev->key())
               {
               case INIT: //打开初始化界面
               {
    //               updatelabel();
                   break;
               }

                default:
               {
                   show_Main();
                   break;
               }
               }
            }
//            else if (event->type()==QEvent::FocusIn)     //然后再判断控件的具体事件 (这里指获得焦点事件)
//                  {
//                          QPushButton *focus_button = (QPushButton *)watched;
//                          focus_button->setStyleSheet("QPushButton{background-color:white;\
//                                                                          color:black;\
//                                                                          border-radius:4px;  border: 1px groove gray; padding:1px 4px;\
//                                                                          border-style: outset;}"
//                                                     );
//                  }
//            else if (event->type()==QEvent::FocusOut)    // 这里指 控件的失去焦点事件
//                 {
//                        QPushButton *focus_button = (QPushButton *)watched;
//                        focus_button->setStyleSheet("QPushButton{color:black;\
//                                                    border-radius:4px;  border: 1px groove gray; padding:1px 4px;\
//                                                    border-style: outset;}");
//                 }
        }
    return 0;
    }

    void init_Show::show_Main()
    {
        QWidget *win = this;
        QWidget *win2 = win;
        while (win->objectName() != "MainWindow") {
            win2 = win;
            win = win->parentWidget();
        }
        win2->deleteLater();
        if(win->isHidden())
            win->show();
    }

    void init_Show::keyPressEvent(QKeyEvent *event)
    {
        if(event->type() == QEvent::KeyPress)
        {
               QKeyEvent *ev = (QKeyEvent *)event;
               switch (ev->key())
               {

               case OK  ://上
               {
                   this->parentWidget()->show();
                   this->close();
                   break;
               }
               default:
              {
                  show_Main();
                  break;
              }
               }
            }

    }
